package com.baizhi.springbootday0201.web.intercaptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自己定义的拦截器 需要告诉springboot这是一个拦截器
 * 就需要实现HandlerInterceptor接口
 */
@Component
public class MyIntercaptor implements HandlerInterceptor {
	/**
	 *
	 * @param request 客户端的请求对象
	 * @param response 响应给客户端的response对象
	 * @param handler  拦截请求的方法
	 * @return true表示请求继续进行，false表示请求拦截
	 * @throws Exception 请求的过程中或者放行的过程中产生的异常，声明抛出
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		System.out.println("拦截请求");
//		return true表示放行
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		System.out.println("拦截请求2");
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		System.out.println("拦截请求3");
	}
}
